//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package events_subscribe

import (
	"fmt"

	"atomgit.com/openlinksaas/proto-gen-go.git/events/events_external_event"
	"atomgit.com/openlinksaas/proto-gen-go.git/events_api"
	"atomgit.com/openlinksaas/proto-gen-go.git/external_events_api"
)

func getEventSourceStr(evSource int32) string {
	if evSource == int32(external_events_api.EVENT_SOURCE_EVENT_SOURCE_GITLAB) {
		return "gitlab"
	} else if evSource == int32(external_events_api.EVENT_SOURCE_EVENT_SOURCE_GITHUB) {
		return "github"
	} else if evSource == int32(external_events_api.EVENT_SOURCE_EVENT_SOURCE_GITEA) {
		return "gitea"
	} else if evSource == int32(external_events_api.EVENT_SOURCE_EVENT_SOURCE_GITEE) {
		return "gitee"
	} else if evSource == int32(external_events_api.EVENT_SOURCE_EVENT_SOURCE_GOGS) {
		return "gogs"
	} else if evSource == int32(external_events_api.EVENT_SOURCE_EVENT_SOURCE_JIRA) {
		return "jira"
	} else if evSource == int32(external_events_api.EVENT_SOURCE_EVENT_SOURCE_CONFLUENCE) {
		return "confluence"
	} else if evSource == int32(external_events_api.EVENT_SOURCE_EVENT_SOURCE_JENKINS) {
		return "jenkins"
	}
	return ""
}

func getUserPolicyStr(userPolicy int32) string {
	if userPolicy == int32(external_events_api.SOURCE_USER_POLICY_SOURCE_USER_POLICY_NONE) {
		return "未设置策略"
	} else if userPolicy == int32(external_events_api.SOURCE_USER_POLICY_SOURCE_USER_POLICY_DISCARD) {
		return "丢弃"
	} else if userPolicy == int32(external_events_api.SOURCE_USER_POLICY_SOURCE_USER_POLICY_MAPPING) {
		return "关联成员"
	} else if userPolicy == int32(external_events_api.SOURCE_USER_POLICY_SOURCE_USER_POLICY_SKIP_MAPPING) {
		return "不记名"
	}
	return ""
}

func processExtEvent(event *events_api.Event, cfgItemList []*CfgItem) (bool, error) {
	var err error
	var msg string

	{
		createEvent := &events_external_event.CreateEvent{}
		if event.EventData.MessageIs(createEvent) {
			err = event.EventData.UnmarshalTo(createEvent)
			if err != nil {
				return false, err
			}
			msg = fmt.Sprintf("%s 在项目 %s 创建第三方接入 %s %s。", event.UserDisplayName, event.ProjectName,
				getEventSourceStr(createEvent.EventSource), createEvent.Title)
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.ExtEvCfg.Create {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}

	{
		updateEvent := &events_external_event.UpdateEvent{}
		if event.EventData.MessageIs(updateEvent) {
			err = event.EventData.UnmarshalTo(updateEvent)
			if err != nil {
				return false, err
			}
			msg = fmt.Sprintf("%s 在项目 %s 更新第三方接入 %s %s(原标题 %s)。", event.UserDisplayName, event.ProjectName,
				getEventSourceStr(updateEvent.EventSource), updateEvent.NewTitle, updateEvent.OldTitle)
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.ExtEvCfg.Update {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}

	{
		getSecretEvent := &events_external_event.GetSecretEvent{}
		if event.EventData.MessageIs(getSecretEvent) {
			err = event.EventData.UnmarshalTo(getSecretEvent)
			if err != nil {
				return false, err
			}
			msg = fmt.Sprintf("%s 在项目 %s 获取 %s %s 密钥。", event.UserDisplayName, event.ProjectName,
				getEventSourceStr(getSecretEvent.EventSource), getSecretEvent.Title)
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.ExtEvCfg.GetSecret {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}

	{
		removeEvent := &events_external_event.RemoveEvent{}
		if event.EventData.MessageIs(removeEvent) {
			err = event.EventData.UnmarshalTo(removeEvent)
			if err != nil {
				return false, err
			}
			msg = fmt.Sprintf("%s 在项目 %s 删除第三方接入 %s %s。", event.UserDisplayName, event.ProjectName,
				getEventSourceStr(removeEvent.EventSource), removeEvent.Title)
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.ExtEvCfg.Remove {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}

	{
		setSourceUserPolicyEvent := &events_external_event.SetSourceUserPolicyEvent{}
		if event.EventData.MessageIs(setSourceUserPolicyEvent) {
			err = event.EventData.UnmarshalTo(setSourceUserPolicyEvent)
			if err != nil {
				return false, err
			}
			msg = fmt.Sprintf("%s 在项目 %s 设置第三方接入 %s %s 用户策略,第三方用户 %s,策略 %s ", event.UserDisplayName, event.ProjectName,
				getEventSourceStr(setSourceUserPolicyEvent.EventSource), setSourceUserPolicyEvent.Title,
				setSourceUserPolicyEvent.SourceDisplayName, getUserPolicyStr(setSourceUserPolicyEvent.UserPolicy))
			if setSourceUserPolicyEvent.MapUserId != "" && setSourceUserPolicyEvent.MapUserDisplayName != "" {
				msg = msg + setSourceUserPolicyEvent.MapUserDisplayName
			}
			msg = msg + "。"
			for _, cfgItem := range cfgItemList {
				if cfgItem.eventCfg.ExtEvCfg.SetSourceUserPolicy {
					sendChatMsg(msg, cfgItem.subItem)
				}
			}
			return true, nil
		}
	}

	return false, nil
}
